<template>
  <div class="news-detail-page">
    <!-- 加载动画 -->
    <div v-if="isLoading" class="loading-overlay animate-fade-in">
      <LoadingAnimation />
    </div>
    
    <!-- 装饰元素 -->
    <div class="absolute top-20 left-10 w-32 h-32 rounded-full bg-gradient-to-r from-indigo-500/10 to-purple-500/10 blur-3xl pointer-events-none"></div>
    <div class="absolute bottom-20 right-10 w-40 h-40 rounded-full bg-gradient-to-r from-blue-500/10 to-indigo-500/10 blur-3xl pointer-events-none"></div>
    
    <!-- 装饰性图标 -->
    <div class="absolute top-10 right-20 opacity-30 pointer-events-none">
      <DecorativeIcon type="star" />
    </div>
    <div class="absolute bottom-10 left-20 opacity-30 pointer-events-none">
      <DecorativeIcon type="lightning" />
    </div>
    <div class="absolute top-1/2 left-5 opacity-30 pointer-events-none">
      <DecorativeIcon type="news" />
    </div>
    <div class="absolute top-1/3 right-5 opacity-30 pointer-events-none">
      <DecorativeIcon type="dot" />
    </div>
    
    <div v-if="!isLoading" class="animate-fade-in">
      <!-- 独立的页面头部 -->
      <header class="page-header">
        <div class="header-content">
          <h1 class="site-title" @click="goHome">新闻聚合平台</h1>
          <button class="close-btn" @click="closePage">
            <span class="close-icon">✕</span>
            关闭
          </button>
        </div>
      </header>

      <!-- 新闻内容区域 -->
      <main class="news-content">
        <div class="content-wrapper animate-slide-up">
          <!-- 新闻标题 -->
          <div class="news-header">
            <div class="relative">
              <div class="absolute -left-2 -top-2 w-8 h-8 rounded-full bg-indigo-500 opacity-10 blur-md"></div>
              <div class="absolute -right-1 -top-1 scale-75 opacity-50 pointer-events-none">
                <DecorativeIcon type="star" :animate="false" />
              </div>
              <span class="news-badge">新闻详情</span>
              <h1 class="news-title">{{ newsData.title }}</h1>
            </div>
          </div>

          <!-- 新闻元信息 -->
          <div class="news-meta">
            <span class="meta-item">
              <span class="meta-icon">📅</span> {{ formatDate(newsData.publishTime) }}
            </span>
            <span class="meta-item">
              <span class="meta-icon">📂</span> {{ newsData.categoryName }}
            </span>
            <span class="meta-item">
              <span class="meta-icon">🏢</span> {{ newsData.sourceName }}
            </span>
          </div>

          <!-- 新闻摘要 -->
          <div v-if="newsData.summary" class="news-summary">
            <div class="relative">
              <div class="absolute -right-2 -top-2 w-6 h-6 rounded-full bg-blue-500 opacity-10 blur-md"></div>
              <h3>摘要</h3>
              <p>{{ newsData.summary }}</p>
            </div>
          </div>

          <!-- 新闻封面图 -->
          <div v-if="newsData.imageUrl" class="news-image">
            <img :src="newsData.imageUrl" :alt="newsData.title" @error="hideImage" />
          </div>

          <!-- 新闻正文 -->
          <div class="news-body">
            <div v-if="newsData.content && newsData.content !== '详细内容...'" class="content-text">
              {{ newsData.content }}
            </div>
            <div v-else class="content-placeholder">
              <p>{{ newsData.summary }}</p>
              <div class="loading-notice">
                <div class="mr-2 inline-block">
                  <DecorativeIcon type="lightning" :animate="true" />
                </div>
                完整内容正在整理中，请稍后查看...
              </div>
            </div>
          </div>

          <!-- 原文链接 -->
          <div class="news-footer">
            <div class="original-link">
              <span>原文链接：</span>
              <a :href="newsData.url" target="_blank" rel="noopener noreferrer" class="source-link">
                {{ newsData.url }}
              </a>
            </div>
          </div>
        </div>
      </main>
    </div>
  </div>
</template>

<script setup>
// 禁用默认布局，确保页面完全独立
definePageMeta({
  layout: false
})

import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { newsAPI } from '~/services/api'
import LoadingAnimation from '~/components/LoadingAnimation.vue'
import DecorativeIcon from '~/components/DecorativeIcon.vue'

const route = useRoute()
const newsData = ref({})
const isLoading = ref(true)

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 隐藏加载失败的图片
const hideImage = (event) => {
  event.target.style.display = 'none'
}

// 返回首页
const goHome = () => {
  window.location.href = '/'
}

// 关闭页面
const closePage = () => {
  window.close()
}

// 加载新闻详情数据
const loadNewsDetail = async (newsId) => {
  try {
    isLoading.value = true
    const data = await newsAPI.getNewsDetail(newsId)
    newsData.value = data
  } catch (error) {
    console.error('加载新闻详情失败:', error)
    newsData.value = {
      title: '新闻未找到',
      summary: `ID为 ${newsId} 的新闻不存在`,
      content: '请返回首页查看其他新闻',
      category_name: '提示',
      source_name: '系统',
      publish_time: new Date().toISOString(),
      url: '#'
    }
  } finally {
    isLoading.value = false
  }
}

// 页面加载时获取新闻数据
onMounted(() => {
  const newsId = parseInt(route.params.id)
  
  if (isNaN(newsId)) {
    newsData.value = {
      title: '无效的新闻ID',
      summary: '请检查URL是否正确',
      content: '无法找到对应的新闻内容',
      category_name: '错误',
      source_name: '系统',
      publish_time: new Date().toISOString(),
      url: '#'
    }
    isLoading.value = false
    return
  }

  // 调用API获取新闻详情
  loadNewsDetail(newsId)
})
</script>

<style scoped>
/* 全局重置 - 确保完全独立 */
.news-detail-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  background: #ffffff;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  overflow-y: auto;
  z-index: 9999;
}

/* 深色模式适配 */

/* 动画效果 */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

@keyframes float {
  0% { transform: translateY(0px); }
  50% { transform: translateY(-10px); }
  100% { transform: translateY(0px); }
}

.animate-fade-in {
  animation: fadeIn 0.5s ease-out;
}

.animate-slide-up {
  animation: slideUp 0.5s ease-out;
}

/* 加载动画 */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.9);
  z-index: 1000;
}


/* 页面头部 */
.page-header {
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
  transition: all 0.3s ease;
}


.header-content {
  max-width: 800px;
  margin: 0 auto;
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.site-title {
  font-size: 1.5rem;
  font-weight: bold;
  color: var(--primary-color);
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 0;
  position: relative;
  display: inline-block;
}

.site-title:hover {
  color: var(--primary-hover);
  transform: translateY(-2px);
}



.close-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: #F3F4F6;
  color: #6B7280;
  border: 1px solid #D1D5DB;
  border-radius: 0.5rem;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: #E5E7EB;
  color: #374151;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}



.close-icon {
  font-size: 1.2rem;
  font-weight: bold;
}

/* 新闻内容区域 */
.news-content {
  padding: 2rem;
}

.content-wrapper {
  max-width: 800px;
  margin: 0 auto;
  background: white;
  border-radius: 1rem;
  padding: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 1px solid rgba(0, 0, 0, 0.05);
}


.news-header {
  margin-bottom: 1.5rem;
  position: relative;
}

.news-badge {
  display: inline-block;
  background: var(--primary-color);
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 1rem;
  font-size: 0.8rem;
  font-weight: 500;
  margin-bottom: 1rem;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.3);
}


.news-title {
  font-size: 2rem;
  font-weight: bold;
  color: #1F2937;
  line-height: 1.3;
  margin: 0;
  transition: all 0.3s ease;
  position: relative;
}


.news-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 2rem;
  padding: 1rem;
  background: #F3F4F6;
  border-radius: 0.5rem;
  transition: all 0.3s ease;
  border: 1px solid rgba(0, 0, 0, 0.05);
}


.meta-item {
  font-size: 0.9rem;
  color: #6B7280;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  transition: all 0.3s ease;
}

.meta-icon {
  display: inline-block;
  transition: transform 0.3s ease;
}

.meta-item:hover .meta-icon {
  transform: scale(1.2);
}


.news-summary {
  margin-bottom: 2rem;
  padding: 1.5rem;
  background: #EFF6FF;
  border-left: 4px solid var(--primary-color);
  border-radius: 0.5rem;
  transition: all 0.3s ease;
  position: relative;
}


.news-summary h3 {
  color: #1E40AF;
  margin-bottom: 0.5rem;
  font-size: 1.1rem;
  transition: all 0.3s ease;
}


.news-summary p {
  color: #374151;
  line-height: 1.6;
  margin: 0;
  transition: all 0.3s ease;
}


.news-image {
  margin-bottom: 2rem;
  text-align: center;
  transition: all 0.3s ease;
}

.news-image img {
  max-width: 100%;
  height: auto;
  border-radius: 0.5rem;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}


.news-body {
  margin-bottom: 2rem;
}

.content-text {
  font-size: 1.1rem;
  line-height: 1.8;
  color: #374151;
  white-space: pre-line;
  transition: all 0.3s ease;
}


.content-placeholder {
  text-align: center;
  padding: 2rem;
  transition: all 0.3s ease;
}

.content-placeholder p {
  font-size: 1.1rem;
  color: #6B7280;
  margin-bottom: 1rem;
  line-height: 1.6;
  transition: all 0.3s ease;
}


.loading-notice {
  background: #FEF3C7;
  color: #92400E;
  padding: 1rem;
  border-radius: 0.5rem;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(251, 191, 36, 0.2);
}


.news-footer {
  border-top: 1px solid #E5E7EB;
  padding-top: 1.5rem;
  transition: all 0.3s ease;
}


.original-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.original-link span {
  color: #6B7280;
  font-weight: 500;
  transition: all 0.3s ease;
}


.source-link {
  color: var(--primary-color);
  text-decoration: none;
  word-break: break-all;
  transition: all 0.3s ease;
  position: relative;
  padding: 2px 4px;
  border-radius: 4px;
}

.source-link:hover {
  color: var(--primary-hover);
  background-color: rgba(var(--primary-rgb), 0.1);
}



/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 1rem;
  }
  
  .site-title {
    font-size: 1.2rem;
  }
  
  .news-content {
    padding: 1rem;
  }
  
  .content-wrapper {
    padding: 1.5rem;
  }
  
  .news-title {
    font-size: 1.5rem;
  }
  
  .news-meta {
    flex-direction: column;
    gap: 0.5rem;
  }
}
</style>